/**
 * 
 */
package com.mindtree.vi.business.authentication;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mindtree.vi.business.javaservice.UserUtil;
import com.mindtree.vi.business.questionsubmission.AuthenticationManager;
import com.mindtree.vi.dbaccess.datatypes.user.User;
import com.mindtree.vi.dbaccess.manager.TableManager;

/**
 * @author venkatesh
 * 
 */
public class SimpleAuthenticationManager implements AuthenticationManager {

	private TableManager<User> tableManager;

	public void authenticate(User user) {

	}

	public boolean authenticate(String emailId, String password) {
		boolean validUser = false;
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("emailId", emailId);
		List<? extends User> credentialsList = tableManager
				.find("select user from User user where user.emailId = :emailId",parameterMap);
		for (User user : credentialsList) {
			if (emailId.equals(user.getEmailId())
					&& password.equals(user.getCredential().getPassword())) {
				UserUtil.setUser(user);
				validUser = true;
			}
		}

		return validUser; // TODO: FIXME!!
	}

	public void setTableManager(TableManager<User> tableManager) {
		this.tableManager = tableManager;
	}
}
